package com.android.test2;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


public class DbAdapter {
	public static final String KEY_CURRENCY = "currency";
    public static final String KEY_VALUE = "value_E";
    public static final String KEY_ROWID = "_id";
    
    private static String[] currencies;
    private static double[] values;
    
    private static final String TAG = "DbAdapter";
    private DatabaseHelper mDbHelper;
    private static SQLiteDatabase mDb;
    
    /**
     * Database creation sql statement
     */
    private static final String DATABASE_CREATE =
            "create table currencies (_id integer primary key autoincrement, "
                    + "currency text not null, value_E real not null);";

    private static final String DATABASE_NAME = "data";
    private static final String DATABASE_TABLE = "currencies";
    private static final int DATABASE_VERSION = 2;
    
        private final Context mCtx;

    private static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(DATABASE_CREATE);
            fillDBInitialData();
        }

        

		@Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS notes");
            onCreate(db);
        }
    }

    /**
     * Constructor - takes the context to allow the database to be
     * opened/created
     * 
     * @param ctx the Context within which to work
     */
    public DbAdapter(Context ctx) {
    	this.mCtx = ctx;
    }

    /**
     * Open the currency database. If it cannot be opened, try to create a new
     * instance of the database. If it cannot be created, throw an exception to
     * signal the failure
     * 
     * @return this (self reference, allowing this to be chained in an
     *         initialization call)
     * @throws SQLException if the database could be neither opened or created
     */
    public DbAdapter open() throws SQLException {
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }
    
    public void close() {
        mDbHelper.close();
    }


    /*
     * InitialData: create the initial data in the database.
     */

    public static long InitialData(String currency, double value){
    	ContentValues initialValues = new ContentValues();
    	initialValues.put(KEY_CURRENCY, currency);
        initialValues.put(KEY_VALUE, value);
    	
    	return mDb.insert(DATABASE_TABLE, null, initialValues);
    }
    
    public static void initData(){
    	/* We can improve the way of get the initial data.
    	 * - We can read an xml file
    	 * - This xml file can be obtain from some web service
         */
    	currencies = new String[] {"USD","Euro","British Pound","Taiwanese New Dollar"};
        values = new double[] {0.704179,1,1.15395,0.0238060};
    }
    
    public static void fillDBInitialData(){
    	initData();
    	
        for (int i=0;i < currencies.length; i++){
        	InitialData(currencies[i], values[i]);
        }
    	
    }
    
    /**
     * Return a Cursor over the list of all currencies in the database
     * 
     * @return Cursor over all notes
     */
    public Cursor fetchAllCurrencies() {
        return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_CURRENCY,
                KEY_VALUE}, null, null, null, null, null);
    }

    /**
     * Return a Cursor positioned at the note that matches the given rowId
     * 
     * @param rowId id of note to retrieve
     * @return Cursor positioned to matching note, if found
     * @throws SQLException if note could not be found/retrieved
     */
    public Cursor fetchNote(long rowId) throws SQLException {

        Cursor mCursor =

                mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
                        KEY_CURRENCY, KEY_VALUE}, KEY_ROWID + "=" + rowId, null,
                        null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;

    }
    
    /**
     * Return a Cursor over the list of all currencies in the database
     * 
     * @return Cursor over all notes
     */
   /* public Cursor fetchValueCurrency() {
    	(String table, 
    	String[] columns, 
    	String selection, 
    	String[] selectionArgs, 
    	String groupBy, 
    	String having, 
    	String orderBy)
        return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_CURRENCY,
                KEY_VALUE}, null, null, null, null, null);
    }*/
    
}
